package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.Users;

public class UsersService {
	String errorString;
	Context context;
	private MessageDialog mMessageDialog;
	private DatabaseHelper DBHelper;

	public UsersService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
	}

	public List<Users> getuserlist() {
		String sql = "select * from t_user_setting order by us_id";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		List<Users> Source = new ArrayList<Users>();
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return Source;
		}
		while (cursor.moveToNext()) {
			Users users = new Users(cursor.getInt(0), cursor.getString(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getInt(5), cursor.getString(6));
			Source.add(users);
		}
		cursor.close();
		return Source;
	}

	public boolean InsertUser(Users user) {

		boolean check = false;
		String sql = "insert into t_user_setting values ";
		String ls_where = "(null,'" + user.getUs_login_id() + "','"
				+ user.getUs_pwd() + "','";
		ls_where += user.getUs_info() + "','" + user.getUs_active() + "',"
				+ user.getUs_color() + ",'" + user.getUs_manager() + "')";

		sql = sql + ls_where;

		errorString = DBHelper.execSQL(sql);

		if (errorString.length() <= 0) {
			check = true;
		}

		return check;
	}

	public boolean UpdateUser(Users user) {

		boolean check = false;
		String sql = "Update t_user_setting set ";
		String ls_where = "us_login_id ='" + user.getUs_login_id()
				+ "',us_pwd ='" + user.getUs_pwd() + "',us_info ='";
		ls_where += user.getUs_info() + "',us_active  ='" + user.getUs_active()
				+ "', us_color = " + user.getUs_color() + ", us_manager = '"
				+ user.getUs_manager() + "'";
		ls_where += " where us_id = " + user.getUs_id();
		sql = sql + ls_where;

		errorString = DBHelper.execSQL(sql);

		if (errorString.length() <= 0) {
			check = true;
		}
		return check;
	}

	public boolean CheckUsers(int li_login_id, int li_id) {

		boolean check = false;
		String sql = "select us_id from t_user_setting where us_login_id  = "
				+ String.valueOf(li_login_id) + " and us_id <> "
				+ String.valueOf(li_id);

		Cursor cursor = DBHelper.rawQuery(sql, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return check;
		}
		if (cursor.moveToNext()) {
			check = true;
		}
		cursor.close();
		return check;

	}

	public boolean CheckUsers(int li_login_id) {
		boolean check = false;
		String sql = "select us_id from t_user_setting where us_login_id  = "
				+ String.valueOf(li_login_id);

		Cursor cursor = DBHelper.rawQuery(sql, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return check;
		}
		if (cursor.moveToNext()) {
			check = true;
		}
		cursor.close();
		return check;

	}

	public boolean login(String Loginid, String pwd) {
		boolean check = false;
		String sql = "select * from t_user_setting where us_login_id = '"
				+ Loginid + "' and us_pwd = '" + pwd + "' and us_active = 'Y'";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return check;
		}
		if (cursor.moveToNext()) {
			check = true;
		}
		cursor.close();
		return check;
	}

	public Users GetUserDetail(int li_user_id) {
		Users user = null;
		String sql = "select * from t_user_setting  where us_id  = "
				+ String.valueOf(li_user_id);

		Cursor cursor = DBHelper.rawQuery(sql, null);
		//user = new Users();
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return null;
		}

		if (cursor.moveToNext()) {

			user = new Users(cursor.getInt(0), cursor.getString(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getInt(5), cursor.getString(6));
		}
		// startManagingCursor(cursor);
		cursor.close();
		return user;
	}

	public Users GetUserDetail(String LoginId) {
		Users user = null;
		String sql = "select * from t_user_setting  where us_login_id  = "
				+ LoginId;

		Cursor cursor = DBHelper.rawQuery(sql, null);
		user = new Users();
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return null;
		}

		if (cursor.moveToNext()) {

			user = new Users(cursor.getInt(0), cursor.getString(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getInt(5), cursor.getString(6));
		}
		// startManagingCursor(cursor);
		cursor.close();
		return user;
	}

	public boolean DeleteUser(int sc_user_id) {

		boolean check = false;
		String sql = "Delete from t_user_setting ";
		String ls_where = "where us_id  = " + sc_user_id;
		sql = sql + ls_where;

		errorString = DBHelper.execSQL(sql);

		if (errorString.length() <= 0) {
			check = true;
		}
		return check;
	}

	public int FindUserId(String LoginId) {
		int i = -1;
		String sql = "select us_id from t_user_setting  where us_login_id  = '"
				+ LoginId + "'";
		Cursor cursor = DBHelper.rawQuery(sql, null);
		if (cursor.moveToNext()) {
			i = cursor.getInt(0);
		}
		cursor.close();
		return i;
	}
	
	public boolean CheckIsManager(String UserId, String pwd) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ DatabaseSchema.UserSetting.SCHEMA_NAME + " where "
				+ DatabaseSchema.UserSetting.LOGIN_ID + " = " + UserId + " and "
				+ DatabaseSchema.UserSetting.PASSWORD + " = " + pwd + " and "
				+ DatabaseSchema.UserSetting.ACTIVE + " = 'Y' and "
				+ DatabaseSchema.UserSetting.MANAGER + " = 'Y'", null);
		if(cursor == null){
			return false;
		}
		if(cursor.moveToNext()){
				cursor.close();
				return true;
		}else{
			cursor.close();
			return false;
		}
	}
	
	public boolean CheckIsManager(String UserId) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ DatabaseSchema.UserSetting.SCHEMA_NAME + " where "
				+ DatabaseSchema.UserSetting.LOGIN_ID + " = " + UserId, null);
		if(cursor == null){
			return false;
		}
		if(cursor.moveToNext()){
			if(cursor.getString(6).equals("Y")){
				cursor.close();
				return true;
			}else{
				cursor.close();
				return false;
			}
		}
		return false;
	}
	
	public int getMaxno() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select max(login_ID) from "
				+ DatabaseSchema.UserSetting.SCHEMA_NAME , null);
		int maxno =0 ;
		if(cursor == null){
			return maxno;
		}
		if(cursor.moveToNext()){
			maxno = cursor.getInt(0);
			
		}
		return maxno;
	}
	
	

	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}

	};
}
